from django.shortcuts import render
from .ml import MachineLearn
from django.http import JsonResponse


# Create your views here.

def index(request):
    return render(request, 'titanic/index.html')


def pred(request):
    """泰坦尼克号生存预测请求"""
    sex = request.POST.get("sex", None)
    age = request.POST.get("age", None)
    alone = request.POST.get("alone", None)
    fare = request.POST.get("fare", None)
    logic_select = request.POST.get("logic_select", None)
    ml = MachineLearn(sex, age, fare, alone)
    if logic_select == "KNN":
        pred, acc = ml.knn()
    elif logic_select == "DecisionTree":
        pred, acc = ml.decisionTree()

    return JsonResponse({"msg": f" 预测的结果: {pred}", "acc": f" 精确度: {acc}"})
